package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.EnvironmentContract;
import com.sunlg.smarthome.data.entity.TempAndHum;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.List;

/**
 * Created by sunlg on 2017/5/12.
 */

public class EnvironmentPresenter implements EnvironmentContract.Presenter {

    private EnvironmentContract.View mView;
    private PreferHelper preferHelper;
    private SHServiceWrapper mSHServiceWrapper;

    public EnvironmentPresenter() {
        preferHelper = new PreferHelper(App.getInstance(), "gateway");
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void loadAllTempAndHum() {
        mSHServiceWrapper.getAllTempAndHum(preferHelper.getInt("id", 0), new MySubscriber<Result<List<TempAndHum>>>() {
            @Override
            public void onSuccess(Result<List<TempAndHum>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.loadAllTemAndHumSuccess(data.getData());
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void loadLatestTemp(int deviceId) {

    }

    @Override
    public void loadLatestHum(int deviceId) {

    }

    @Override
    public void attachView(EnvironmentContract.View view) {
        this.mView = view;
    }

    @Override
    public void dettachView() {
        this.mView = null;
    }
}
